package com.scenic.rd_android.threerd.chart.customchart;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.nova.scenic.projectlibs.util.lifecyclehelper.LifeCycleHelper;
import com.nova.scenic.projectlibs.util.lifecyclehelper.LifeCycleHelperBase;
import com.scenic.rd_android.BaseActivity;
import com.scenic.rd_android.R;

import java.util.ArrayList;
import java.util.List;

public class TestCharts extends BaseActivity {


    LineChart lineChart;

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        LifeCycleHelper origin = getLifeCycleHelper();
        setLifeCycleHelper(new MyLifeCycleHelper1(origin));
//        setLifeCycleHelper(new TestSimpleLineChart(origin));

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    class TestSimpleLineChart extends LifeCycleHelperBase {

        public TestSimpleLineChart(LifeCycleHelper helper) {
            super(helper);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test_charts);
            lineChart = (LineChart) findViewById(R.id.id_lineChart1);
            lineChart.setData(getLineData());
        }

        public LineData getLineData() {

            LineData lineData = new LineData();

            lineData.addDataSet(getDataSet1());
            lineData.addDataSet(getDataSet2());
            return lineData;
        }

        public ILineDataSet getDataSet1() {
            List<Entry> yVals = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                yVals.add(new Entry(i, i * i));
            }


            LineDataSet dataSet = new LineDataSet(yVals, "x");
            dataSet.setColor(Color.RED);

            return dataSet;
        }

        public ILineDataSet getDataSet2() {

            List<Entry> yVals = new ArrayList<>();
            LineDataSet dataSet = new LineDataSet(yVals, "y");
            dataSet.setColor(Color.BLUE);
            for (int i = 0; i < 10; i++) {
                yVals.add(new Entry(i, 10 * (float) Math.sin(i)));
            }
            return dataSet;
        }
    }

    class MyLifeCycleHelper1 extends LifeCycleHelperBase {

        public MyLifeCycleHelper1(LifeCycleHelper helper) {
            super(helper);
        }

        private void setData(int count, float range) {

            ArrayList<Entry> yVals1 = new ArrayList<Entry>();

            for (int i = 0; i < count; i++) {
                float mult = range / 2f;
                float val = (float) (Math.random() * mult) + 50;// + (float)
                // ((mult *
                // 0.1) / 10);
                yVals1.add(new Entry(i, val));
            }

            ArrayList<Entry> yVals2 = new ArrayList<Entry>();

            for (int i = 0; i < count; i++) {
                float mult = range;
                float val = (float) (Math.random() * mult) + 450;// + (float)
                // ((mult *
                // 0.1) / 10);
                yVals2.add(new Entry(i, val));
            }

            LineDataSet set1, set2;

            if (lineChart.getData() != null &&
                    lineChart.getData().getDataSetCount() > 0) {
                set1 = (LineDataSet) lineChart.getData().getDataSetByIndex(0);
                set2 = (LineDataSet) lineChart.getData().getDataSetByIndex(1);
                set1.setValues(yVals1);
                set2.setValues(yVals2);
                lineChart.getData().notifyDataChanged();
                lineChart.notifyDataSetChanged();
            } else {
                // create a dataset and give it a type
                set1 = new LineDataSet(yVals1, "DataSet 1");

                set1.setAxisDependency(YAxis.AxisDependency.LEFT);
                set1.setColor(ColorTemplate.getHoloBlue());
                set1.setCircleColor(Color.WHITE);
                set1.setLineWidth(2f);
                set1.setCircleRadius(3f);
                set1.setFillAlpha(65);
                set1.setFillColor(ColorTemplate.getHoloBlue());
                set1.setHighLightColor(Color.rgb(244, 117, 117));
                set1.setDrawCircleHole(false);
                //set1.setFillFormatter(new MyFillFormatter(0f));
                //set1.setDrawHorizontalHighlightIndicator(false);
                //set1.setVisible(false);
                //set1.setCircleHoleColor(Color.WHITE);

                // create a dataset and give it a type
                set2 = new LineDataSet(yVals2, "DataSet 2");
                set2.setAxisDependency(YAxis.AxisDependency.RIGHT);
                set2.setColor(Color.RED);
                set2.setCircleColor(Color.WHITE);
                set2.setLineWidth(2f);
                set2.setCircleRadius(3f);
                set2.setFillAlpha(65);
                set2.setFillColor(Color.RED);
                set2.setDrawCircleHole(false);
                set2.setHighLightColor(Color.rgb(244, 117, 117));
                //set2.setFillFormatter(new MyFillFormatter(900f));

                ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
                dataSets.add(set1); // add the datasets
                dataSets.add(set2);

                // create a data object with the datasets
                LineData data = new LineData(dataSets);
                data.setValueTextColor(Color.WHITE);
                data.setValueTextSize(9f);

                // set data
                lineChart.setData(data);
            }
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test_charts);
            lineChart = (LineChart) findViewById(R.id.id_lineChart1);
            lineChart.setOnChartGestureListener(new OnChartGestureListener() {
                @Override
                public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {

                }

                @Override
                public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {

                }

                @Override
                public void onChartLongPressed(MotionEvent me) {

                }

                @Override
                public void onChartDoubleTapped(MotionEvent me) {

                }

                @Override
                public void onChartSingleTapped(MotionEvent me) {

                }

                @Override
                public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {

                }

                @Override
                public void onChartScale(MotionEvent me, float scaleX, float scaleY) {

                }

                @Override
                public void onChartTranslate(MotionEvent me, float dX, float dY) {

                }
            });

            lineChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
                @Override
                public void onValueSelected(Entry e, Highlight h) {

                }

                @Override
                public void onNothingSelected() {

                }
            });

            lineChart.setDrawGridBackground(false);

            lineChart.setDescription("");
            lineChart.setNoDataTextDescription("You need to provide data for the chart.");

            // enable touch gestures
            lineChart.setTouchEnabled(true);

            // enable scaling and dragging
            lineChart.setDragEnabled(true);
            lineChart.setScaleEnabled(true);
            // mChart.setScaleXEnabled(true);
            // mChart.setScaleYEnabled(true);

            // if disabled, scaling can be done on x- and y-axis separately
            lineChart.setPinchZoom(true);

            // set an alternative background color
            // mChart.setBackgroundColor(Color.GRAY);


            setData(20, 30);

            lineChart.animateX(2500);

            // get the legend (only possible after setting data)
            Legend l = lineChart.getLegend();

            // modify the legend ...
            // l.setPosition(LegendPosition.LEFT_OF_CHART);
            l.setForm(Legend.LegendForm.LINE);
//        l.setTypeface(mTfLight);
            l.setTextSize(11f);
            l.setTextColor(Color.WHITE);
            l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
//        l.setYOffset(11f);

            XAxis xAxis = lineChart.getXAxis();
//        xAxis.setTypeface(mTfLight);
            xAxis.setTextSize(11f);
            xAxis.setTextColor(Color.WHITE);
            xAxis.setDrawGridLines(false);
            xAxis.setDrawAxisLine(false);

            YAxis leftAxis = lineChart.getAxisLeft();
//        leftAxis.setTypeface(mTfLight);
            leftAxis.setTextColor(ColorTemplate.getHoloBlue());
//        leftAxis.setAxisMaximum(200f);
//        leftAxis.setAxisMinimum(0f);
            leftAxis.setDrawGridLines(true);
            leftAxis.setGranularityEnabled(true);

            YAxis rightAxis = lineChart.getAxisRight();
//        rightAxis.setTypeface(mTfLight);
            rightAxis.setTextColor(Color.RED);
//        rightAxis.setAxisMaximum(900);
//        rightAxis.setAxisMinimum(-200);
            rightAxis.setDrawGridLines(false);
            rightAxis.setDrawZeroLine(false);
            rightAxis.setGranularityEnabled(false);

        }
    }


}
